# coding=utf-8

import wx
import os
import time
import json
import requests
from lxml import etree

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36',
}
path = 'videos'

def go():
    if not os.path.exists(path):
        os.mkdir(path)
    
    urls = ['https://www.pornhub.com/video?o=tr','https://www.pornhub.com/video?o=ht']
    for url in urls:
        try:
            list_page(url)
        except Exception as err:
            print(err)

def list_page(url):
    print('crawling : %s' % url)
    resp = requests.get(url, headers=headers)
    html = etree.HTML(resp.text)
    vkeys = html.xpath('//*[@class="phimage"]/div/a/@href')
    gif_keys = html.xpath('//*[@class="phimage"]/div/a/img/@data-mediabook')
    _list = []
    for i in range(len(vkeys)):
        item = {}
        item['vkey'] = vkeys[i].split('=')[-1]
        item['gif_url'] = gif_keys[i]
        _list.append(item)
        try:
            if 'ph' in item['vkey']:
                url = 'https://www.pornhub.com/view_video.php?viewkey=%s' % item['vkey']
                print(url)
                req_detail_page(url)
        except Exception as err:
            print(err)

def req_detail_page(url):
    s = requests.Session()
    resp = s.get(url, headers=headers)
    html = etree.HTML(resp.content)
    title = ''.join(html.xpath('//h1//text()')).strip()
    js = html.xpath('//*[@id="player"]/script/text()')[0]
    n1 = js.find('{')
    n2 = js.find('var player_mp4_seek')
    tem = js[n1:n2].strip().strip(';')
    con = json.loads(tem)
    for _dict in con['mediaDefinitions']:
        if 'quality' in _dict.keys():
            if _dict.get('videoUrl'):
                print(_dict.get('quality'), _dict.get('videoUrl'))
                try:
                    downloadvideo(_dict.get('videoUrl'), title)
                    break
                except Exception as err:
                    print(err)

def downloadvideo(url, title):
    r = requests.get(url, stream=True)  # here we need to set stream = True parameter
    with open(path + '/%s.mp4' % title, 'wb') as f:
        for chunk in r.iter_content(chunk_size=512):
            if chunk:  # filter out keep-alive new chunks
                f.write(chunk)
                f.flush()
        f.close()
    print('download video success ! %s %s' % (url, title))

def show_message(msg):
    pass

'''
if __name__ == '__main__':
    run()
'''
#import the newly created GUI file 
import main  
class MainFrame(main.MyFrame1): 
    def __init__(self,parent): 
        main.MyFrame1.__init__(self,parent) 
        
    def run(self,event):
        go()
        
    def show_message(self,msg):
        message = self.m_textCtrl1.GetValue().encode('utf-8')
        message = message + msg + '\r\n'
        self.m_textCtrl1.SetValue(message)
    
app = wx.App(False) 
frame = MainFrame(None) 
frame.Show(True) 
#start the applications 
app.MainLoop()  
        